import pygame
import projectile
import time
import random
import math

class Note(projectile.Projectile):
    def __init__(self,position,bar,bar_loc):
        super(Note,self).__init__()
        self.img = pygame.Surface((75,100),pygame.SRCALPHA)
        self.sprite_sheet = pygame.image.load("../media/images/Notes.png").convert_alpha()
        self.mode = random.random()*4//1
        self.rect = self.img.get_rect()
        self.rect.centery = position-100
        self.rect.centerx = 1250
        self.status = "sad"
        self.happy = False
        self.happiness = 0
        if self.mode == 0:
            self.velocity = -5
            self._vConst = -5
        elif self.mode == 1:
            self.velocity = -3
            self._vConst = -3
        elif self.mode == 2:
            self.velocity = -7
            self._vConst = -7
        elif self.mode == 3:
            self.velocity = -2
            self._vConst = -2
        self.locY=position-100
        self.barPar=bar
        if self.mode == 3:
            self.up_down = 0
        else:
            self.up_down = ((bar_loc//5)+1)%2
    def update(self,time):
        if self.happy == True and self.status == "sad":
            self.status = "happy"
        self.velocity = self._vConst * time
        self.rect.y = self.barPar.curveLoc(self.rect.centerx)-self.barPar.locY+self.locY
        if self.up_down == 1:
            self.rect.y += 75
        self.rect.move_ip(self.velocity,0)
    def draw(self,screen):
        self.img=self.sprite_sheet
        screen.blit(self.img,self.rect,pygame.Rect((150*self.mode)+self.happiness*75,0+(150*self.up_down),75,150))
    def collided(self,bullet_list = []):
        if self.rect.left <= 250:
            return 2
        elif len(bullet_list) > 0 and self.status == "sad":
            if self.up_down == 0:
                collision_rect = pygame.Rect(self.rect.centerx-21,self.rect.centery+50,42,20)
            else:
                collision_rect = pygame.Rect(self.rect.centerx,self.rect.centery-20,42,20)
            for bullet in bullet_list:
                if collision_rect.colliderect(bullet.rect):
                    bullet_list.remove(bullet)
                    del bullet
                    self.happiness = 1
                    self.happy = True
                    return 1
        else:
            return 0
    def get_point_value(self):
        if self.rect.centerx < 400:
            if self.mode == 0:
                return 100
            elif self.mode == 1:
                return 75
            elif self.mode == 2:
                return 200
            elif self.mode == 3:
                return 50
        elif self.rect.centerx < 650:
            if self.mode == 0:
                return 50
            elif self.mode == 1:
                return 35
            elif self.mode == 2:
                return 100
            elif self.mode == 3:
                return 25
        elif self.rect.centerx < 950:
            if self.mode == 0:
                return 25
            elif self.mode == 1:
                return 20
            elif self.mode == 2:
                return 50
            elif self.mode == 3:
                return 13
        else:
            if self.mode == 0:
                return 10
            elif self.mode == 1:
                return 7
            elif self.mode == 2:
                return 20
            elif self.mode == 3:
                return 5
    def spawn_powerup(self):
        probability = random.random()
        if probability < 0.05:   # ink blast
            return 1
        elif probability < 0.1:  # time dilation (slow down)
            return 2
        elif probability < 0.15: # heatlh powerup
            return 3
        elif probability < 0.2: # time slingshot (speed up)
            return 4
        else:
            return 5             # nothing at all
    def shuffle_bar(self,new_bar):
        self.barPar.copy(new_bar)
        self.locY = self.barPar.locY-125
        self.rect.y = math.fabs(self.barPar.curveLoc(self.rect.centerx)+self.locY-self.barPar.locY)
    def get_col_rect(self):
        if self.up_down == 0:
            return  pygame.Rect(self.rect.centerx-21,self.rect.centery+50,42,20)
        else:
            return pygame.Rect(self.rect.centerx,self.rect.centery-20,42,20)